
###############################################################################
########## Figure 2: Inequality & Critical Juncture in China for R & R ########

# Set your working directory
getwd ()
setwd("/Users/yongjaekim/Documents/Papers/CCP Control2")

library("ggplot2")
library("openxlsx")
library("rio")
library ("scales")

Mydata1 <- read.xlsx("SinoGini4.xlsx")
Mydata1

# change the specific linetypes and shapes: http://www.cookbook-r.com/Graphs/Shapes_and_line_types/
ggp <- ggplot(Mydata1, aes(x=Year, y=Inequality, group = Index)) +
  geom_line(aes(linetype=Index), # Line type depends on cond
            size = 1.0) +       # Thicker line
  geom_point(aes(shape=Index),   # Shape depends on cond
             size = 3.0) +        # Large points
  scale_shape_manual(values=c(8,5,4)) +                  # Change shapes
  scale_linetype_manual(values=c("solid", "dotted", "dotdash")) # Change linetypes
ggp

ggp <- ggp + theme_classic() + theme(panel.border = element_rect(linetype = "solid", fill = "NA")) # panel border: https://ggplot2.tidyverse.org/reference/theme.html    
ggp

ggp <- ggp + geom_vline(xintercept = c(1958, 1966, 1977, 1989, 1997, 2008, 2014), linetype="dashed") + 
  annotate("text", label = "Great Leap Forward (1958)", size = 3, angle = "90", x = 1959, y = 25 ) +
  annotate("text", label = "Cultural Revolution (1966)", size = 3, angle = "90", x = 1967, y = 25 ) +
  annotate("text", label = "11th Party Congress (1977)", size = 3, angle = "90", x = 1978, y = 25 ) +
  annotate("text", label = "Tibetan & Tiananmen Protests (1989)", size = 3, angle = "90", x = 1990, y = 25 ) +
  annotate("text", label = "Uyghurs' March (1997)", size = 3, angle = "90", x = 1998, y = 25) +
  annotate("text", label = "Tibetan March (2008)", size = 3, angle = "90", x = 2009, y = 25) +
  annotate("text", label = "Hong Kong's Umbrella Movement (2014)", size = 3, angle = "90", x = 2015, y =25 ) +
  scale_x_continuous(breaks = c(1950, 1955, 1960, 1965, 1970, 1975, 1980, 1985, 1990, 1995, 2000, 2005, 2010, 2015, 2020))
ggp


###############################################################################
########## Figure 3: Gini, Position, Party, and Valence issues ########

Mydata1 <- read.xlsx("GiniIssues5.xlsx")
Mydata1

library (ggplot2)

# change the specific linetypes and shapes: http://www.cookbook-r.com/Graphs/Shapes_and_line_types/
ggp <- ggplot(Mydata1, aes(x=Year, y=Percentage, group = Issues_and_Index)) +
  geom_line(aes(linetype=Issues_and_Index), # Line type depends on cond
            size = 1.0) +       # Thicker line
  geom_point(aes(shape=Issues_and_Index),   # Shape depends on cond
             size = 3.0) +        # Large points
  scale_shape_manual(values=c(8,5,4,0)) +                  # Change shapes
  scale_linetype_manual(values=c("solid", "dotted","dotted", "solid")) # Change linetypes
ggp

ggp <- ggp + theme_classic() + theme(panel.border = element_rect(linetype = "solid", fill = "NA")) # panel border: https://ggplot2.tidyverse.org/reference/theme.html    
ggp

ggp <- ggp + geom_vline(xintercept = c(1958, 1966, 1977, 1989, 1997, 2008, 2014), linetype="dashed") + 
  annotate("text", label = "Great Leap Forward (1958)", size = 5, angle = "90", x = 1959, y = 25 ) +
  annotate("text", label = "Cultural Revolution (1966)", size = 5, angle = "90", x = 1967, y = 25 ) +
  annotate("text", label = "11th Party Congress (1977)", size = 5, angle = "90", x = 1978, y = 25 ) +
  annotate("text", label = "Tibetan & Tiananmen Protests (1989)", size = 5, angle = "90", x = 1990, y = 25 ) +
  annotate("text", label = "Uyghurs' March (1997)", size = 5, angle = "90", x = 1998, y = 25) +
  annotate("text", label = "Tibetan March (2008)", size = 5, angle = "90", x = 2009, y = 25) +
  annotate("text", label = "Hong Kong's Umbrella Movement (2014)", size = 5, angle = "90", x = 2015, y =25 ) +
  scale_x_continuous(breaks = c(1955, 1960, 1965, 1970, 1975, 1980, 1985, 1990, 1995, 2000, 2005, 2010, 2015, 2020)) +
  scale_y_continuous(breaks = c(0, 10, 20, 30, 40, 50, 60, 70, 80))

ggp

#####################
### Correlation #####
Mydata1 <- read.xlsx("GiniIssues4.xlsx")
Mydata1

# Position Issues
r<-cor.test(Mydata1$giniindex, Mydata1$positionalissues, method = c("pearson"))
r

r<-cor.test(Mydata1$giniindex, Mydata1$commandeconomy, method = c("pearson"))
r
r<-cor.test(Mydata1$giniindex, Mydata1$socialistmovement, method = c("pearson"))
r
r<-cor.test(Mydata1$giniindex, Mydata1$maoism, method = c("pearson"))
r
r<-cor.test(Mydata1$giniindex, Mydata1$communism, method = c("pearson"))
r

# Valence Issues
r <-cor.test(Mydata1$giniindex, Mydata1$valenceissues, method = c("pearson"))
r

r<-cor.test(Mydata1$giniindex, Mydata1$economicdevelopment, method = c("pearson"))
r
r<-cor.test(Mydata1$giniindex, Mydata1$consistentdevelopment, method = c("pearson"))
r

# Party Issues
r <-cor.test(Mydata1$giniindex, Mydata1$partyissues, method = c("pearson"))
r

r<-cor.test(Mydata1$giniindex, Mydata1$listofpartyelites, method = c("pearson"))
r
r<-cor.test(Mydata1$giniindex, Mydata1$partyannouncement, method = c("pearson"))
r
r<-cor.test(Mydata1$giniindex, Mydata1$politicaldiscipline, method = c("pearson"))
r
r<-cor.test(Mydata1$giniindex, Mydata1$poleconreform, method = c("pearson"))
r

########################################################
###### Figure A1: Stacked Bar Chart of Documents #######

# Set your working directory
getwd ()
setwd("/Users/yongjae/Desktop/Papers/CCP Control2")

library("ggplot2")
library("openxlsx")
library("rio")
library ("scales")

Mydata1 <- read.xlsx("StackedBarChart2.xlsx")
Mydata1


## https://stackoverflow.com/questions/64267754/plotting-a-time-series-stacked-bar-chart
Type_colors <- c(Party_Document = "grey10", Editorial = "grey60")
barchart <-ggplot()
barchart <- barchart + geom_col(data = Mydata1, aes (x = Year, y = Number, fill = Type), position = "stack", width = 0.8) + 
  theme_bw() + theme(legend.position = 'bottom') + 
  scale_x_continuous(breaks = c(1921, 1945, 1956, 1969, 1973, 1977, 1982, 1987, 1992, 1997, 2002, 2007, 2012, 2017))

barchart <- barchart + scale_fill_manual(values = Type_colors)  
barchart

colours ()











